package com.xunua.NaoZiKongBai.LoginActivity;

import android.content.Context;
import android.content.SharedPreferences;

import com.xunua.NaoZiKongBai.Base.BaseModel;
import com.xunua.NaoZiKongBai.Bean.Bmob.MyUser;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

/**
 * @user XUNUA
 * @email xunua@outlook.com
 * @creat time 2020/5/13 17:50
 */
public class LoginModel extends BaseModel<LoginPresenter,LoginContract.M> {

    public LoginModel(LoginPresenter mPresenter) {
        super(mPresenter);
    }

    @Override
    public LoginContract.M getContract() {
        return new LoginContract.M() {
            @Override
            public void requestLogin(String name, String pwd, Context context){
                //验证账户是否被封禁
                BmobQuery<MyUser> query = new BmobQuery<>();
                query.addWhereEqualTo("username",name).findObjects(new FindListener<MyUser>() {
                    @Override
                    public void done(List<MyUser> list, BmobException e) {
                        if (e == null) {
                            if (list.size() == 1) {
                                if (!list.get(0).isBan()) {
                                    MyUser user = new MyUser();
                                    user.setUsername(name);
                                    user.setPassword(pwd);
                                    user.login(new SaveListener<MyUser>() {
                                        @Override
                                        public void done(MyUser myUser, BmobException e) {
                                            if (e == null) {
//                                                //登录成功后将登录的设备id绑定到数据库中
                                                new Thread(new Runnable() {
                                                    @Override
                                                    public void run() {
                                                        SharedPreferences sp = context.getSharedPreferences("userlogin", Context.MODE_PRIVATE);//只是为了在重新登录的时候展示当前登录的账号
                                                        SharedPreferences.Editor edit = sp.edit();
                                                        edit.putString("name",name);
                                                        edit.putString("pwd",pwd);
                                                        String token = String.valueOf((long) Math.pow(((Math.random()+1) * 10), 10));//随机数的十次方   //小于十四位数
                                                        edit.putString("token",token);
                                                        MyUser userlogin = new MyUser();
                                                        userlogin.setToken(token);
                                                        BmobUser bmobUser = BmobUser.getCurrentUser(MyUser.class);
                                                        userlogin.update(bmobUser.getObjectId(), new UpdateListener() {
                                                            @Override
                                                            public void done(BmobException e) {
                                                                if (e == null) {
                                                                    //上传令牌Token

                                                                    //登录成功
                                                                    mPresenter.getContract().responseLoginResult(true,false);
                                                                }else {
                                                                    //更新失败

                                                                    //登录失败
                                                                    mPresenter.getContract().responseLoginResult(false,false);
                                                                }
                                                            }
                                                        });
                                                        edit.commit();
                                                    }
                                                }).start();
                                            }else {
                                                //登录失败
                                                mPresenter.getContract().responseLoginResult(false,false);
                                            }
                                        }
                                    });
                                }else {
                                    //账号已被封禁
                                    mPresenter.getContract().responseLoginResult(false,true);
                                }
                            }else {
                                mPresenter.getContract().responseLoginResult(false,false);
                            }
                        }else {
                            mPresenter.getContract().responseLoginResult(false,false);
                        }
                    }
                });
            }
        };
    }
}
